//
//  AfanWelcomeController.swift
//  weiboOfAfan
//
//  Created by afan on 16/3/24.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class AfanWelcomeController: UIViewController {

    
    var iconViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        
        
        //在发送请求前先使用本地图片,有就是用本地图片,没有就使用默认的
        if let urlString = AfanUserAccountViewModel.shardInstace.userAccount?.avatar_large{
            let url = NSURL(string: urlString)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        AfanUserAccountViewModel.shardInstace.loadUserInfo { (error) -> () in
            if error != nil{
                print("请求用户数据出错")
                return
            }
            
            if let urlString = AfanUserAccountViewModel.shardInstace.userAccount?.avatar_large {
                let url = NSURL(string: urlString)!
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
        
        
    }
    
    private func prepareUI() {
        // 1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 头像
        // centerX
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        
        // 底部约束
        // 记录底部约束
        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewBottomCons!)
        
        // 欢迎label
        self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
    
    
    //界面显示时
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    private func moveAnimation() {
        UIView.animateWithDuration(3, delay: 0.2, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
//                print("动画完成,切换界面")
                UIView.animateWithDuration(1.2, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        AppDelegate.outSwitchRootViewController(AfanMainController())
                })
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // MARK: - 懒加载
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()
}
